package com.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class RegistrationDatabase {
	private DataBaseConnection dataBaseConnection = null;
	private ResultSet resultSet=null;
	private Connection connection = null;
	private Statement statement=null;
	PreparedStatement preparedStatement=null;
	public RegistrationDatabase() {
		dataBaseConnection = new DataBaseConnection();
		connection = dataBaseConnection.CallConnection();
	}

	public String Registration(RegistrationVO registrationVO) {
		String result=null;
		try
		{
			boolean status=true;
		
			String customerName=registrationVO.getCustomerName();
			String accountNumber=registrationVO.getAccountNumber();
			String balance=registrationVO.getBalance();
			String emailAddress=registrationVO.getEmailAddress();
			String password=registrationVO.getPassword();
			statement=connection.createStatement();
			resultSet=statement.executeQuery("select * from ACCOUNT");
			while(resultSet.next())
			{
				if(resultSet.getString("CUSTOMERNAME").equals(customerName))
				{
					status=false;
					break;
				}
			}
			if(status)
			{
				preparedStatement=connection.prepareStatement("insert into ACCOUNT(CUSTOMERNAME,ACCOUNTNUMBER,BALANCE,EMAILADDRESS,PASSWORD) values(?,?,?,?,?)");	
				preparedStatement.setString(1, customerName);
				preparedStatement.setString(2, accountNumber);
				preparedStatement.setString(3, balance);
				preparedStatement.setString(4, emailAddress);
				preparedStatement.setString(5, password);
				preparedStatement.executeUpdate();
				result="Registered sucessfully";
			}
			else
			{
				result="Already Registered ";
			}
			
			
		}
		catch (Exception e) {
			
			e.printStackTrace();
		}
		

return result;
	}
}
